﻿using MongoDB.Bson;
using Sustainalytics.Common.Hosting;
using Sustainalytics.Common.Services;
using Sustainalytics.Entities;
using Sustainalytics.ScreeningTool.BL;
using Sustainalytics.ScreeningTool.Service.Map;
using Sustainalytics.Utils.Serilog;
using Sustainalytics.WebApiHosting;


namespace Sustainalytics.ScreeningTool.Service
{
    public class ScreeningToolApplication : ServiceApplication
    {

        public ScreeningToolApplication() : base()
        {
           
        }

        public ScreeningToolApplication(IBasicLogger logger)  : base(logger)
        {
           
        }

        protected override void AddStartupServices()
        {
            RegisterService(new WebApiServiceDefinition<Startup>(HostConfig.GetHostUrl(), "screeningTool"));

            SetupMappings();
        }


        private void SetupMappings()
        {
            var runResultMap = new RunResultMap();
            ServiceProvider.Add<IMap<RunResultDto, BsonDocument>>(runResultMap);

            var criteriaResultMap = new CriteriaResultMap();
            ServiceProvider.Add<IMap<CriteriaResultDto, CriteriaResultOutput>>(criteriaResultMap);
        }
       
        protected override bool HandleStartException(System.Exception e)
        {
            CommonEventSerilog.Log.ApplicationStartError(e);
            return false;
        }

        protected override bool HandleStopException(System.Exception e)
        {
            CommonEventSerilog.Log.ApplicationStopError(e);
            return false;
        }

        protected override void OnUnhandledDomainException(System.Exception e)
        {
            CommonEventSerilog.Log.OnUnhandledDomainException(e);
        }
    }
}
